<template>
    <van-nav-bar title="编辑收货地址" left-text="" left-arrow @click-left="goBack" />
    <van-address-edit :area-list="areaList" show-set-default show-delete :address-info="info"
        :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" @delete="onDelete" />
</template>
<script setup>
import { areaList } from "@vant/area-data";
const goBack = () => {
    history.back();
};
const router = useRouter();
const route = useRoute();
const { cookies } = useCookies();
const LoginUser = reactive(cookies.get("LoginUser") ?? {});
const addressid = ref(route.query.addressid ?? 0);

const info = ref({});//详情
onBeforeMount(() => {
    getInfoData();
});
const getInfoData = async () => {
    let result = await Api.addressInfo({
        busid: LoginUser.id,
        addressid: addressid.value,
    });
    if (result.code == 1) {
        info.value = {
            name: result.data.consignee,
            tel: result.data.mobile,
            addressDetail: result.data.address,
            areaCode: result.data.district,
            isDefault: result.data.status == "1" ? true : false,
        };
    }
};

const onSave = async (val) => {
    let data = {
        busid: LoginUser.id,
        consignee: val.name,
        code: val.areaCode,
        mobile: val.tel,
        address: val.addressDetail,
        status: val.isDefault == true ? "1" : "0",
        addressid: addressid.value,
    };
    let result = await Api.addressEdit(data);
    if (result.code == 1) {
        showSuccessToast({
            message: result.msg,
            onClose: function () {
                router.push("/business/address/index");
            },
        });
    } else {
        showFailToast(result.msg);
    }
};
const onDelete = () => {
    showConfirmDialog({
        title: "删除",
        message: "是否确认删除收货地址？",
    })
        .then(async () => {
            let result = await Api.addressDel({
                busid: LoginUser.id,
                addressid: addressid.value,
            });
            if (result.code == 1) {
                showSuccessToast({
                    message: result.msg,
                    onClose: function () {
                        router.push("/business/address/index");
                    },
                });
            } else {
                showFailToast(result.msg);
            }
        })
        .catch(() => { });
};

</script>